package com.jinlejia.erp.adapter;

/**
 * Created by Administrator on 2016/9/1 0001.
 */

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jinlejia.erp.R;
import com.utils.PathToUriToFileUtils;

import java.util.List;


public class PhotoAdapter extends
        RecyclerView.Adapter<PhotoAdapter.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> mDatas;

    public PhotoAdapter(Context context, List<String> datats) {
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
        mContext = context;
    }

    public void setDatas(List<String> datats) {
        this.mDatas = datats;
        this.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        int count = mDatas.size();
        if (count < 10) {
            count++;
        }
        return count;
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_photo,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.mImg = (ImageView) view.findViewById(R.id.id_item_image);
        viewHolder.remove = (ImageView) view.findViewById(R.id.id_remove);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        if (position < mDatas.size()) {
            Uri uri;
            if (mDatas.get(position).contains("http://")) {
                uri = Uri.parse(mDatas.get(position));
            } else {
                Uri imageContentUri = PathToUriToFileUtils.getImageContentUri(mContext, mDatas.get(position));
                uri = imageContentUri;
            }
            Glide.with(mContext)
                    .load(uri)
                    .placeholder(R.mipmap.ic_no_img)
                    .centerCrop()
                    .error(R.mipmap.ic_no_img)
                    .into(viewHolder.mImg);
            viewHolder.remove.setVisibility(View.VISIBLE);
            viewHolder.remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnDelClickLitener.onDelClick(position);
                }
            });
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(position);
                }
            });
        } else {
            viewHolder.remove.setVisibility(View.GONE);
            viewHolder.mImg.setImageResource(R.mipmap.ic_add_img);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnSelectClickLitener.onItemClick();
                }
            });
        }

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        ImageView mImg;
        ImageView remove;
    }

    /**
     * 添加图片回调接口
     */
    public interface OnSelectClickLitener {
        void onItemClick();
    }

    private OnSelectClickLitener mOnSelectClickLitener;

    public void setOnSelectClickLitener(OnSelectClickLitener mOnSelectClickLitener) {
        this.mOnSelectClickLitener = mOnSelectClickLitener;
    }

    /**
     * 点击图片回调接口
     */
    public interface OnItemClickLitener {
        void onItemClick(int i);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    /**
     * 添加图片回调接口
     */
    public interface OnDelClickLitener {
        void onDelClick(int p);
    }

    private OnDelClickLitener mOnDelClickLitener;

    public void setOnDelClickLitener(OnDelClickLitener mOnDelClickLitener) {
        this.mOnDelClickLitener = mOnDelClickLitener;
    }

}
